package com.yycwxt.modules.admin.core_flows;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yycwxt.domain.Flows;
import com.yycwxt.modules.admin.store.StoreService2;
import lombok.SneakyThrows;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Created by Mr.Zhang on 2022/9/6 16:14
 */
@Service
@Transactional
public class FlowsServiceTest extends ServiceImpl<FlowsMapper, Flows> {

    // 根据id修改

    // 根据id删除

    // Execl导入数据
    @SneakyThrows
    @Transactional
    public boolean importFileToDB(boolean deleteHistory, MultipartFile file) {
        if (deleteHistory) {
            this.remove(null);
        }
        EasyExcel.read(file.getInputStream(), Flows.class, new ReadListener<Flows>() {

            private static final int BATCH_SIZE = 1000;
            private List<Flows> ls = Lists.newArrayList();

            @Override
            public void invoke(Flows data, AnalysisContext context) {
                // System.out.println(data);
                ls.add(data);
                if (ls.size() >= BATCH_SIZE) {
                    FlowsServiceTest.this.saveBatch(ls);
                    ls.clear();
                }
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                FlowsServiceTest.this.saveBatch(ls);
            }
        }).sheet().doRead();
        return true;
    }
}
